import config from '@/config'
import { deep, deDupArray } from '@/utils'
import mescrollMixin from '@/commons/ZmMescrollUni/mescroll-mixins'
const LIMIT = config.pageSizeKey
const PAGE = config.pageNumKey
// const TOTAL = config.totalKey
// const LIST = config.pageListKey
export default {
  data() {
    return {
      total: 0,
      pageInfo: {
        [LIMIT]: 10,
        [PAGE]: 1
      },
      //   _pageInfo: {},
      isPageInit: true,
      resData: {},
      listData: [] //列表数据
    }
  },
  mixins: [mescrollMixin],
  created() {
    this._pageInfo = deep(this.pageInfo)
  },
  methods: {
    downCallback(e) {
      this.mescroll && this.mescroll.resetUpScroll()
    },
    upCallback(page) {
      if (typeof this.listLoad === 'function') {
        this.pageInfo = {
          [LIMIT]: page.size,
          [PAGE]: page.num
        }
        if (this.beforeList()) return
        const time = new Date().getTime() + 500
        return this.listLoad(this.pageInfo)
          .then((res) => {
            this.afterList(res, this.pageInfo, page)
            let { count, list } = res
            this.getData(list)
            //兼容没有分页的时候
            if (!count && !list) {
              count = res.length
              list = res
            }
            if (page.num === 1) {
              // console.log('1页时候')
              // console.log(count, list)
              this.listData = []
              this.$nextTick(() => {
                this.listData = list
              })
            } else {
              const listData = [...this.listData, ...res.list]
              this.listData = this.pkId
                ? deDupArray(listData, this.pkId)
                : listData
              this.afterLoadList(res, this.pageInfo, page)
            }
            // this.mescroll.endBySize(0, 0)
            if (this.isPageInit) {
              this.$nextTick(() => {
                this.isPageInit = false
              })
            }
            if (this.mescroll.isDownScrolling) {
              setTimeout(() => {
                this.mescroll.endBySize(list.length, count)
              }, time - new Date().getTime())
            } else {
              this.mescroll.endBySize(list.length, count)
            }
          })
          .catch(() => {
            this.mescroll.endErr()
          })
      }
      console.error('请重写 listLoad 函数，获取数据')
    },
    getData(list) {},
    listLoad() {},
    beforeList() {},
    afterList() {},
    afterLoadList() {},
    endLoading() {
      console.log('this.mescroll', this.mescroll)
      this.mescroll &&
        setTimeout(() => {
          this.mescrollFun('endSuccess')
        }, 500)
    },
    mescrollFun: function (type) {
      // this.mescroll.apply(this)
      // this.mescroll[type](arguments.length)
    }
  }
}
